package com.yanqu.road.dao.impl.beautyescort;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.beautyescort.rank.EscortWeekUserValue;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EscortWeekUserValueDaoImpl extends TempDataObjectDao<EscortWeekUserValue> {

    public static String getEscortWeekUserValueSql = "select * from `t_u_escort_week_user_value` where `weekId`=? or `weekId`=?";
    public Map<Integer, Map<Long, EscortWeekUserValue>> getEscortWeekUserValue(int preWeekId, int thisWeekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Long, EscortWeekUserValue>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getEscortWeekUserValueSql);
            int i = 1;
            call.setInt(i++, preWeekId);
            call.setInt(i++, thisWeekId);
            rs = call.executeQuery();
            while (rs.next()) {
                EscortWeekUserValue data = parse(rs);
                if(!result.containsKey(data.getWeekId())){
                    result.put(data.getWeekId(), new ConcurrentHashMap<>());
                }
                result.get(data.getWeekId()).put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertEscortWeekUserValueSql = "insert into `t_u_escort_week_user_value`(userId,weekId,getReward,getRewardTime,value,unionUid,reward,position,modifyTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(EscortWeekUserValue data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPosition()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getModifyTime()));
        if (getDb().executeNoneQuery(insertEscortWeekUserValueSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateEscortWeekUserValueSql = "update `t_u_escort_week_user_value` set getReward=?,getRewardTime=?,value=?,unionUid=?,reward=?,position=?,modifyTime=? where weekId=? and userId=?";
    @Override
    protected boolean updateData(EscortWeekUserValue data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPosition()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getModifyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateEscortWeekUserValueSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected EscortWeekUserValue parseData(ResultSet rs) throws SQLException {
        EscortWeekUserValue data = new EscortWeekUserValue();
        data.setUserId(rs.getLong("userId"));
        data.setWeekId(rs.getInt("weekId"));
        data.setGetReward(rs.getBoolean("getReward"));
        data.setGetRewardTime(rs.getLong("getRewardTime"));
        data.setValue(rs.getLong("value"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setReward(rs.getString("reward"));
        data.setPosition(rs.getInt("position"));
        data.setModifyTime(rs.getLong("modifyTime"));

        return data;
    }
}
